# -*- coding:utf-8 -*-
# @FileName :human_player.py
# @Created  :2024/9/29
# @Author   :天空之城
from game.actions import PokerActions
from game.player.player_base import Player


class HumanPlayer(Player):
    def __init__(self, id, name, chips):
        super().__init__(id, name, chips)
        self.is_human = True

    def get_action(self, highest_bet):
        """
        收集人类玩家的输入决定
        :param highest_bet: 当前最高的下注金额
        :return: 返回玩家的决定 ('弃牌'/'跟注'/'加注'/'全押'/'过牌')
        """
        while True:
            # 获取玩家当前筹码数量
            player_chips = self.chips

            # 构建可操作选项列表
            actions = []

            if highest_bet <= player_chips:
                actions.append('过牌(check)')
                actions.append('跟注(call)')
                actions.append('加注(raise)')
                actions.append('全押(all-in)')
            else:
                actions.append('全押(all-in)')

            if highest_bet == self.current_bet:
                actions.remove('跟注(call)')
            # 过牌只有在最高下注为0时可以选择
            if highest_bet != 0 and self.current_bet != highest_bet:
                actions.remove('过牌(check)')
            actions.append('弃牌(fold)')
            actions_str = '/'.join(actions)

            decision = input(
                f"{self.name}, 当前最高下注为 {highest_bet} 筹码。您当前下注筹码：{self.current_bet}"
                f"\n请选择操作({actions_str}): ").strip().lower()

            if decision in ['弃牌', 'fold', '跟注', 'call', '加注', 'raise', '全押', 'all-in', '过牌', 'check']:
                if decision in ['fold', '弃牌']:
                    self.fold()
                    return PokerActions.FOLD
                elif decision in ['call', '跟注'] and '跟注(call)' in actions:
                    self.call(highest_bet)
                    return PokerActions.CALL
                elif decision in ['raise', '加注'] and '加注(raise)' in actions:
                    raise_amount = self.get_raise_amount(highest_bet)
                    self.raise_bet(raise_amount)
                    return PokerActions.RAISE
                elif decision in ['all-in', '全押'] and '全押(all-in)' in actions:
                    self.all_in()
                    return PokerActions.ALL_IN
                elif decision in ['check', '过牌'] and '过牌(check)' in actions:
                    self.has_acted = True  # 过牌也是一种行动
                    return PokerActions.CHECK

            print("无效输入，请重新选择。")

    def get_raise_amount(self, highest_bet):
        """
        获取玩家的加注金额
        """
        while True:
            try:
                amount = int(
                    input(f"玩家 {self.name}，请输入您的加注金额（大于 {highest_bet} 且不超过 {self.chips}）: ").strip())
                if amount > highest_bet and amount <= self.chips:
                    return amount
                elif amount <= highest_bet:
                    print(f"加注金额必须大于当前最高下注金额 {highest_bet}，请重新输入。")
                elif amount > self.chips:
                    print(f"加注金额不能超过您当前的筹码 {self.chips}，请重新输入。")
            except ValueError:
                print("无效的金额，请输入一个整数。")
